//
//  JZCameraShootingToolBar.m
//
//  Created by liujz on 31/05/2018.
//  Copyright © 2018 liujz. All rights reserved.
//

#import "JZCameraShootingToolBar.h"
#import "JZCameraShootingButton.h"
#import <Masonry/Masonry.h>

#define WeakObj(o) @autoreleasepool{} __weak typeof(o) o##Weak = o;
#define CLOSE_BTN_IMG   @"icon_cancel"
#define OK_BTN_IMG      @"btn_sure"
#define RETURN_BTN_IMG  @"btn_return"


@interface JZCameraShootingToolBar() <shootingButtonDelegate>

@property (nonatomic, strong) JZCameraShootingButton * shootingButton;
@property (nonatomic, strong) UIButton * closeButton;
@property (nonatomic, strong) UIButton * leftButton;
@property (nonatomic, strong) UIButton * rightButton;
@property (nonatomic, strong) UIButton * editorButton;

@end


@implementation JZCameraShootingToolBar

- (id)initWithFrame:(CGRect)frame TapEnable:(BOOL) tapEnable LongPressEnable:(BOOL) longPressEnable {
    
    if (self = [super initWithFrame:frame]) {
        self.tapEnable          = tapEnable;
        self.longPressEnable    = longPressEnable;
        
        [self setingSubViews];
    }
    
    return self;
}

/**
 设置子控件
 */
- (void)setingSubViews {
    
    [self addSubview:self.shootingButton];
    [self addSubview:self.closeButton];
    [self addSubview:self.leftButton];
    [self addSubview:self.rightButton];

    [self addSubview:self.editorButton];
    
    [self.shootingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(85.0f));
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
        make.height.equalTo(@(85.0f));
    }];
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(40.0f));
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(20.0f);
        make.height.equalTo(@(40.0f));
    }];
    
    self.leftButton.frame = CGRectMake((self.center.x - 30.0f), (self.frame.size.height * 0.5f)-30.0f, 60.0f, 60.0f);
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(-30.0f);
        make.centerY.equalTo(self);
    }];
    
    self.rightButton.frame = CGRectMake((self.center.x - 30.0f), (self.frame.size.height * 0.5f)-30.0f, 60.0f, 60.0f);
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(-30.0f);
        make.centerY.equalTo(self);
    }];
    
    self.editorButton.frame = CGRectMake((self.center.x - 30.0f), (self.frame.size.height * 0.5f)-30.0f, 60.0f, 60.0f);
    [self.editorButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(-30.0f);
        make.centerY.equalTo(self);
    }];

}

/**
 小中心录制，拍照按钮

 @return ShootingButton
 */
- (JZCameraShootingButton *)shootingButton {
    
    if (!_shootingButton) {
        _shootingButton = [JZCameraShootingButton getShootingButtonWithTapEnable:self.tapEnable LongPressEnable:self.longPressEnable];
        _shootingButton.shootingButtonrDelegate = self;
    }
    return _shootingButton;
}

/**
 关闭按钮

 @return UIButton
 */
- (UIButton *)closeButton {
    
    if (!_closeButton) {
        _closeButton = [[UIButton alloc]init];
        [_closeButton setImage:[UIImage imageNamed:CLOSE_BTN_IMG inBundle:CAMERA_CURRENT_BUNDLE compatibleWithTraitCollection:nil] forState:normal];
        _closeButton.tag = 3;
        [_closeButton addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

/**
 左侧按钮
 
 @return UIButton
 */
- (UIButton*)leftButton {
    
    if (!_leftButton) {
        _leftButton = [[UIButton alloc]init];
        _leftButton.hidden = YES;
        _leftButton.tag = 1;
        [_leftButton setImage:[UIImage imageNamed:RETURN_BTN_IMG inBundle:CAMERA_CURRENT_BUNDLE compatibleWithTraitCollection:nil] forState:normal];
        _leftButton.layer.cornerRadius = 30;
        _leftButton.layer.masksToBounds = YES;
        [_leftButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

/**
 右侧按钮
 
 @return UIButton
 */
- (UIButton*)rightButton {
    
    if (!_rightButton) {
        _rightButton = [[UIButton alloc]init];
        _rightButton.hidden = YES;
        _rightButton.layer.cornerRadius = 30;
        _rightButton.tag = 4;
        _rightButton.layer.masksToBounds = YES;
        [_rightButton setImage:[UIImage imageNamed:OK_BTN_IMG inBundle:CAMERA_CURRENT_BUNDLE compatibleWithTraitCollection:nil] forState:normal];
        [_rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

/**
 编辑按钮
 
 @return UIButton
 */
- (UIButton*)editorButton {
    
    if (!_editorButton) {
        _editorButton = [[UIButton alloc]init];
        _editorButton.hidden = YES;
        [_editorButton setBackgroundColor:[UIColor lightTextColor]];
        _editorButton.layer.cornerRadius = 30;
        _editorButton.tag = 4;
        _editorButton.layer.masksToBounds = YES;
        [_editorButton addTarget:self action:@selector(editorButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _editorButton;
}

#pragma mark - actionClick 事件逻辑
// 关闭按钮
- (void)closeClick:(UIButton*)button {
    
    if ([self.shootingToolBarDelegate respondsToSelector:@selector(shootingToolBarAction:buttonIndex:)]) {
        [self.shootingToolBarDelegate shootingToolBarAction:self buttonIndex:button.tag];
    }
}

// 左侧取消按钮
- (void)leftButtonClick:(UIButton*)button {
    
     [self buttonAnimation:NO];
    if ([self.shootingToolBarDelegate respondsToSelector:@selector(shootingToolBarAction:buttonIndex:)]) {
        [self.shootingToolBarDelegate shootingToolBarAction:self buttonIndex:button.tag];
    }
}

// 右侧确定按钮
- (void)rightButtonClick:(UIButton*)button {
    
    if ([self.shootingToolBarDelegate respondsToSelector:@selector(shootingToolBarAction:buttonIndex:)]) {
        [self.shootingToolBarDelegate shootingToolBarAction:self buttonIndex:button.tag];
    }
}

// 编辑按钮
- (void)editorButtonClick:(UIButton *)button {
    
    if ([self.shootingToolBarDelegate respondsToSelector:@selector(shootingToolBarAction:buttonIndex:)]) {
        [self.shootingToolBarDelegate shootingToolBarAction:self buttonIndex:button.tag];
    }
}


/**
 左侧和右侧按钮的动画效果

 @param open 打开还是关闭
 */
- (void)buttonAnimation:(BOOL)open {
    
    _closeButton.hidden         = open;
    _shootingButton.hidden      = open;
    
    if (self.editEnable) {
        _editorButton.hidden    = !open;
    } else {
        _editorButton.hidden    = YES;
    }
    
    _leftButton.hidden          = !open;
    _rightButton.hidden         = !open;
    
    if (open) {
        WeakObj(self);
        [UIView animateWithDuration:0.4 animations:^{
            selfWeak.leftButton.transform   = CGAffineTransformTranslate(selfWeak.leftButton.transform, -120, 0);
            selfWeak.rightButton.transform  = CGAffineTransformTranslate(selfWeak.rightButton.transform, 120, 0);
        }];
        
        return;
    }
    //隐藏
    _leftButton.transform   = CGAffineTransformIdentity;
    _rightButton.transform  = CGAffineTransformIdentity;
    
}


#pragma mark - 中心按钮的代理方法

/**
 停止录制

 @param button ShootingButton 对象
 @param type 拍照，还是录制
 */
- (void)shootingStop:(JZCameraShootingButton *)button shootingType:(shootingType)type {
    
    [self buttonAnimation:YES];
     button.hidden = YES;
    // 回调自己的代理
    if ([self.shootingToolBarDelegate respondsToSelector:@selector(shootingStop:shootingType:)]) {
        [self.shootingToolBarDelegate shootingStop:self shootingType:type];
    }
 
}

/**
 开始录制

 @param button 对象
 @param type 拍照，录制类型
 @param value 录制有进度值
 */
- (void)shootingStarting:(JZCameraShootingButton *)button shootingType:(shootingType)type progress:(CGFloat)value {
    
    // 回调自己的代理
    if ([self.shootingToolBarDelegate respondsToSelector:@selector(shooingStart:actionType:progress:)]) {
        [self.shootingToolBarDelegate shooingStart:self actionType:type progress:value];
    }
}


@end




